package com.ws.util;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * websercie客户端调用工具类
 *
 * @author dolojia
 * @ClassName WebserviceClientUtils
 * @Date 2020/9/7 13:42
 */
@Slf4j
public class WebserviceClientUtils {

    /**
     * websercie客户端调用
     *
     * @param wsdUrl
     * @param operationName
     * @param params
     * @return com.alibaba.fastjson.JSONObject
     * @Author dolojia
     * @Date 2020/9/7 13:44
     */
    public static String callWebService(String wsdUrl, String operationName, String... params) {
        Object[] objects = new Object[0];
        try {
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            Client client = dcf.createClient(wsdUrl);
            //如果存在加密情况的话
            //client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke(operationName, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info(wsdUrl + ",webservice服务返回信息{}", JSON.toJSONString(objects[0]));
        return JSON.toJSONString(objects[0]);
    }

}
